package com.woniuxy.oldmanapi.controller;

import com.woniuxy.oldmanapi.entity.Building;
import com.woniuxy.oldmanapi.entity.Older;
import com.woniuxy.oldmanapi.service.BedService;
import com.woniuxy.oldmanapi.service.OlderService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.xvo.OlderVo;
import com.woniuxy.oldmanapi.xvo.StoreyDyaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.woniuxy.oldmanapi.entity.Bed;
import com.woniuxy.oldmanapi.service.BedService;
import com.woniuxy.oldmanapi.vo.BedVo;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BedController {
    @Autowired
    BedService bedService;
    @Autowired
    OlderService olderService;

    @GetMapping("/bed/list/{id}")
    public ResponseResult<List<Bed>> list(@PathVariable("id") int id) {
        ResponseResult<List<Bed>> result = new ResponseResult<>();
        try {
            List<Bed> lists = bedService.findBeds(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }

    //根据楼栋号查询所有床位信息
    @GetMapping("/bed/selectBedDyaVoByBuid")
    public ResponseResult<List<StoreyDyaVo>> selectBedDyaVoByBuid(int buildingId) {
        ResponseResult<List<StoreyDyaVo>> result = new ResponseResult<>();
        try {
            List<StoreyDyaVo> storeyList = bedService.selectBedDyaVoByBuid(buildingId);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(storeyList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //查询床位数
    @GetMapping("/bed/selectBedAccount")
    public ResponseResult<Map<String, Integer>> selectBedAccount() {
        ResponseResult<Map<String, Integer>> result = new ResponseResult<>();
        try {
            int allBedAccount = bedService.selectBedAccount(-1);
            int useAccount = bedService.selectBedAccount(1);
            int freeAccount = bedService.selectBedAccount(0);
            Map<String, Integer> map = new HashMap<>();
            map.put("allBedAccount", allBedAccount);
            map.put("useAccount", useAccount);
            map.put("freeAccount", freeAccount);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    @GetMapping("/bed/num/{id}")
    public ResponseResult<Integer> num(@PathVariable("id") int id) {
        ResponseResult<Integer> result = new ResponseResult<>();
        try {
            int a = bedService.findBedNum(id);
            result.setCode(200);
            result.setMsg("查数成功!");
            result.setData(a);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查数失败!");
        }
        return result;
    }

    //查询所有楼栋信息
    @GetMapping("/bed/selectAllBuilding")
    public ResponseResult<List<Building>> selectAllBuilding() {
        ResponseResult<List<Building>> result = new ResponseResult<>();
        try {
            List<Building> buildingList = bedService.selectAllBuilding();
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(buildingList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }


    //查看空闲床位信息
    @GetMapping("/bed/searchStoreyByState0")
    public ResponseResult<List<StoreyDyaVo>> searchStoreyByState0(int buildingId) {
        ResponseResult<List<StoreyDyaVo>> result = new ResponseResult<>();
        try {
            List<StoreyDyaVo> storeyList = bedService.searchStoreyByState0(buildingId);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(storeyList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //查看入住床位信息
    @GetMapping("/bed/searchStoreyByState1")
    public ResponseResult<List<StoreyDyaVo>> searchStoreyByState1(int buildingId) {
        ResponseResult<List<StoreyDyaVo>> result = new ResponseResult<>();
        try {
            List<StoreyDyaVo> storeyList = bedService.searchStoreyByState1(buildingId);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(storeyList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //根据床位号查询老者详细信息
    @GetMapping("/bed/searchOlderByBid")
    public ResponseResult<OlderVo> searchOlderByBid(int bedId) {
        ResponseResult<OlderVo> result = new ResponseResult<>();
        try {
            OlderVo older = olderService.searchOlderByBid(bedId);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(older);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    @PostMapping("/bed/add")
    public ResponseResult<Void> add(@RequestBody BedVo bedVo) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            Bed bed = new Bed();
            bed.setRoomId(bedVo.getRoomId());
            bed.setStoreyId(bedVo.getStoreyId());
            bed.setBuidingId(bedVo.getBuidingId());
            bed.setBedname(bedVo.getBedPrefix() + '-' + bedVo.getBedname());
            bed.setState(0);
            bedService.add(bed);
            result.setCode(200);
            result.setMsg("添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result;
    }

    @PutMapping("/bed/modify")
    public ResponseResult<Void> modify(@RequestBody Bed bed) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            bedService.modify(bed);
            result.setCode(200);
            result.setMsg("修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result;
    }

    @DeleteMapping("/bed/delete/{id}")
    public ResponseResult<Void> delete(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            Bed bed = bedService.selectOne(id);
            if (bed.getState() == 1) {
                result.setCode(202);
                result.setMsg("床位已有入住，无法删除!");
            } else {
                bedService.delete(id);
                result.setCode(200);
                result.setMsg("删除成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result;
    }

    @GetMapping("/bed/one/{id}")
    public ResponseResult<Bed> one(@PathVariable("id") int id) {
        ResponseResult<Bed> result = new ResponseResult<>();
        try {
            Bed bed = bedService.selectOne(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(bed);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
}

